﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BuzzwordBingo.Interface;
using BuzzwordBingo.Interface.API;

namespace BuzzwordBingo.Game
{
    /// <summary>
    /// Creates DTO's from an implementation object
    /// </summary>
    class DtoFactory
    {
        #region public static methods

        /// <summary>
        /// Creates BuzzwordCategoryDto
        /// </summary>
        /// <param name="BuzzwordCategory">implementation object</param>
        /// <returns>DTO</returns>
        public static BuzzwordCategoryDto CreateBuzzwordCategoryDto(IBuzzwordCategory BuzzwordCategory)
        {
            BuzzwordCategoryDto buzzCategory = new BuzzwordCategoryDto();
            buzzCategory.Category = BuzzwordCategory.Category;
            buzzCategory.GetId = BuzzwordCategory.GetId;
            return buzzCategory;
        }

        /// <summary>
        /// Creates a list of BuzzwordCategoryDto
        /// </summary>
        /// <param name="BuzzwordCategoryList">list of implementation objects</param>
        /// <returns>DTO</returns>
        public static IList<BuzzwordCategoryDto> CreateBuzzwordCategoryListDto(IList<IBuzzwordCategory> BuzzwordCategoryList)
        {
            IList<BuzzwordCategoryDto> categoryList = new List<BuzzwordCategoryDto>();
            foreach (IBuzzwordCategory category in BuzzwordCategoryList)
                categoryList.Add(CreateBuzzwordCategoryDto(category));
            return categoryList;
        }

        /// <summary>
        /// Creates BuzzwordDto
        /// </summary>
        /// <param name="BuzzWord">implementation object</param>
        /// <returns>DTO</returns>
        public static BuzzwordDto CreateBuzzwordDto(IBuzzWord BuzzWord)
        {
            BuzzwordDto buzzwordDto = new BuzzwordDto();
            buzzwordDto.FuzzyWord = BuzzWord.FuzzyWord;
            buzzwordDto.IsMarked = BuzzWord.IsMarked;
            return buzzwordDto;
        }

        /// <summary>
        /// Creates BingocardDto
        /// </summary>
        /// <param name="BingoCard">implementation object</param>
        /// <returns>DTO</returns>
        public static BingocardDto CreateBingocardDto(IBingoCard BingoCard)
        {
            BingocardDto bingocardDto = null;
            if (BingoCard != null)
            {
                int jDimension = BingoCard.BuzzWords.GetUpperBound(0) + 1;
                int iDimension = BingoCard.BuzzWords.GetUpperBound(1) + 1;
                bingocardDto = new BingocardDto();
                for (int j = 0; j < jDimension; j++)
                    for (int i = 0; i < iDimension; i++)
                        bingocardDto.BuzzWords[i + j * jDimension] = CreateBuzzwordDto(BingoCard.BuzzWords[i, j]);
            }
            return bingocardDto;
        }

        /// <summary>
        /// Creates ParticipantDto
        /// </summary>
        /// <param name="Participant">implementation object</param>
        /// <returns>DTO</returns>
        public static ParticipantDto CreateParticipantDto(IParticipant Participant)
        {
            ParticipantDto participantDto = null;
            if (Participant != null)
            {
                participantDto = new ParticipantDto();
                participantDto.Email = Participant.Email;
                participantDto.MyBingoCard = CreateBingocardDto(Participant.MyBingoCard);
            }
            return participantDto;
        }

        /// <summary>
        /// Creates ParticipantDtoList
        /// </summary>
        /// <param name="ParticipantList">implementation object</param>
        /// <returns>DTO</returns>
        public static IList<ParticipantDto> CreateParticipantDtoList(IList<IParticipant> ParticipantList)
        {
            IList<ParticipantDto> participantDtoList = new List<ParticipantDto>();
            foreach (IParticipant p in ParticipantList)
                participantDtoList.Add(CreateParticipantDto(p));
            return participantDtoList;
        }

        /// <summary>
        /// Creates BingoEventDto
        /// </summary>
        /// <param name="BingoEvent">implementation object</param>
        /// <returns>DTO</returns>
        public static BingoEventDto CreateBingoEventDto(IBingoEvent BingoEvent)
        {
            BingoEventDto bingoEventDto = new BingoEventDto();
            bingoEventDto.StartDate = BingoEvent.StartDate;
            bingoEventDto.EndDate = BingoEvent.EndDate;
            bingoEventDto.Speaker = BingoEvent.Speaker;
            bingoEventDto.Organiser = BingoEvent.Organiser;
            bingoEventDto.Category = CreateBuzzwordCategoryDto(BingoEvent.Category);
            bingoEventDto.ParticipantList = CreateParticipantDtoList(BingoEvent.ParticipantList);
            bingoEventDto.GetId = BingoEvent.GetId;
            return bingoEventDto;
        }

        #endregion
    }
}
